﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using TrilhaImovel.Core.Entities;
using System.Globalization;
using TrilhaImovel.Core.Repositories;
using AutoMapper;
using TrilhaImovel.UI.Controllers.JSONTransfer;
using TrilhaImovel.UI.Controllers.Properties;

namespace TrilhaImovel.UI.Controllers
{
    [HandleError]
    public class MapController : Controller
    {
        private readonly IEstateRepository estateRepository;

        public MapController(IEstateRepository repository)
        {
            estateRepository = repository;
        }

        public ActionResult ViewProperties(decimal latitude, decimal longitude)
        {
            var format = new CultureInfo("en-US");

            // Keeps values with decimal point because they will be used by javascript functions.
            ViewData["InitialPointLat"] = latitude.ToString(format);
            ViewData["InitialPointLng"] = longitude.ToString(format);

            ViewData["DistanceInMeters"] = Settings.Default.FilterDistanceDefaultInMeters;
            ViewData["FilterDistanceMinInMeters"] = Settings.Default.FilterDistanceMinInMeters;
            ViewData["FilterDistanceMaxInMeters"] = Settings.Default.FilterDistanceMaxInMeters;

            return View();
        }
                
        [HttpPost]
        public JsonResult GetRentMarkersAsync(BoundFilter bound) //BoundSquare boundSquare
        {
            IList<Estate> estates = estateRepository.GetEstates(bound.LatLeft, bound.LngLeft, bound.LatRight, bound.LngRight);

            var jsonResult = (from estate in estates
                              select new
                              {
                                  Id = estate.Id,
                                  PointLocation = new 
                                  {
                                    Latitude = estate.PointLocation.Latitude,
                                    Longitude = estate.PointLocation.Longitude,
                                  },
                                  PriceOrRentValue = estate.PriceOrRentValue.ToString(new CultureInfo("pt-BR")),
                                  NumberOfRooms = estate.NumberOfRooms,
                                  AreaInSquareMeter = estate.AreaInSquareMeter,
                                  Address = new
                                  {
                                      Street = estate.Address.Street,
                                      Neighborhood = estate.Address.Neighborhood,
                                      City = estate.Address.City,
                                      State = estate.Address.State,
                                      ZipCode = estate.Address.ZipCode
                                  },
                                  OperationType = ConvertEnums.DisplayOperationType(estate.OperationType),
                                  PropertyType = ConvertEnums.DisplayPropertyType(estate.PropertyType),
                                  MarkerType = estate.MarkerType.ToString(),
                                  RealEstateAgency = new
                                  {
                                      Id = estate.RealStateAgency.Id,
                                      Name = estate.RealStateAgency.Name
                                  }
                              }).ToList();

            return Json(jsonResult);               
                      
        }

        [HttpPost]
        public void GetInterestPointsAsync()
        {
            //IList<Estate> estates = estateRepository.GetEstates();
            
            //Mapper.CreateMap<Estate, MarkerModel>();
            //IList<MarkerModel> viewModelList =
            //    Mapper.Map<IList<Estate>, IList<MarkerModel>>(estates);
            
            //return Json(viewModelList);                
        }        
    }
}
